<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-10-13
 * Time: 21:56:47
 * Info:
 */

namespace plugin\huiblog\app\model;

use think\facade\Db;
use support\Cache;

class Article
{

    protected $table = 'blog_article';

    /**
     * 自动时间戳类型
     * @var string
     */
    protected $autoWriteTimestamp = true;

    /**
     * 添加时间
     * @var string
     */
    protected $createTime = 'create_time';

    /**
     * 更新时间
     * @var string
     */
    protected $updateTime = 'update_time';

    /**
     * 获取文章列表
     *
     * @param $cate_id  栏目id（字符串，多条用逗号分割）
     * @param $type     属性id（字符串，多条用逗号分割）
     * @param $thumb    是否有缩略图
     * @param $num      每页数量
     * @param $order    排序
     * @param $is_home  是否首页展示
     *
     */
    function lists(
        $cate_id = '',
        $type = '',
        $thumb = 0,
        $num = 10,
        $order = 'sort asc,update_time desc',
        $is_home = 0,
        $keyword = '',
        $cid = '',
        $tid = ''
    ) {
        //随机排序
        $map   = [];
        $map[] = ['status', '=', 1];
        if ( ! empty($cate_id)) {
            $map[] = ['article_cate_id', 'in', ($cate_id)];
        }
        if ( ! empty($type)) {
            $map[] = ['type', 'in', ($type)];
        }
        if ( ! empty($thumb)) {
            $map[] = ['thumb', '<>', ''];
        }
        if ( ! empty($is_home)) {
            $map[] = ['is_home', '=', $is_home];
        }
        if ( ! empty($keyword)) {
            $map[] = ['title', 'like', "%".$keyword."%"];
        }
        if ( ! empty($cid)) {
            $map[] = ['article_cate_id', '=', $cid];
        }
        if ( ! empty($tid)) {
            $getAid = Db::table('blog_article_keywords')->where('keywords_id', $tid)->column('aid');
            $map[]  = ['id', 'in', $getAid];
        }

        if ( ! empty($order) && $order == 'rand()') {
            $list = Db::table('blog_article')->field('id,title,thumb,create_time,read,desc')->where($map)->orderRaw("rand()")->paginate($num);
        } else {
            $list = Db::table('blog_article')->field('id,title,thumb,create_time,read,desc')->where($map)->order($order)->paginate($num);
        }
        $list->each(function ($item) {
            //关键字
            $keyword_array = Db::table('blog_article_keywords')->field('k.id,k.title')->alias('a')->leftJoin(['blog_keywords' => 'k'],
                'k.id = a.keywords_id')->where(array(
                'a.aid'    => $item['id'],
                'k.status' => 1
            ))->limit(5)->select()->toArray();
            foreach ($keyword_array as $key => $keyword) {
                $keyword_array[$key]['url'] = "/blog/lists?tag_id=".$keyword['id'];
            }

            $item['url']           = "/blog/article/id/".$item['id'];
            $item['keyword_array'] = $keyword_array;

            return $item;
        });

        return $list;
    }

    /**
     * 获取博文总数量
     */
    public function count()
    {
        if (Cache::get('blogArticleCount')) {
            $res = Cache::get('blogArticleCount');
        } else {
            $res = Db::table('blog_article')->where(['status' => 1, 'delete_time' => 0])->count();
            Cache::set('blogArticleCount', $res);
        }

        return $res ?? 0;
    }

    public function hotArtices()
    {
        $res = Db::table('blog_article')->where(['status' => 1])->order('read desc')->limit(10)->select();

        return $res ?? [];
    }

}
